// pages/receiptStudentSelectList/receiptStudentSelectList.js
let util = require('../../utils/util.js')
let bxt = require('../../utils/bxt.js')
let noticeModel = require('../../models/notice.js')
let receiptModel = require('../../models/receipt.js')

var param 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    relations:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    param = getApp().globalData.param

    this.makeReceiptHashMapData(param.notice.myReceipts)
    this.initView(param.userRole.relations)

    getApp().notificationCenter.register(bxt.RECEIPT_SUCCESSED_MESSAGE, this, "handleReceiptMessage");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.removeReceiptHashMapData(param.notice.myReceipts)
    getApp().notificationCenter.remove(bxt.RECEIPT_SUCCESSED_MESSAGE, this)
    param = null
  },

  makeReceiptHashMapData:function(receipts) {
    if (receipts) {
      var length = receipts.length
      for (var index = 0; index<length; index++) {
        util.hashMap.Set(receipts[index].student.sid, receipts[index])
      }
    }
  },

  removeReceiptHashMapData: function (receipts) {
    if (receipts) {
      var length = receipts.length
      for (var index = 0; index < length; index++) {
        util.hashMap.Remove(receipts[index].student.sid)
      }
    }
  },

  handleReceiptMessage:function(object) {
    this.makeReceiptHashMapData(param.notice.myReceipts)
    this.initView(param.userRole.relations)
  },

  initView:function(relations) {
    if (relations) {
      var length = relations.length
      for (var index = 0; index < length; index++) {
        if (util.hashMap.Contains(relations[index].student.sid)) {
          relations[index].student.receipted = true
        }else {
          relations[index].student.receipted = false
        }
      }

      if (noticeModel.RECEIPT_REQUIREMENT[0].value != param.notice.receiptRequirement) {
        this.setData({
          receiptButtonDesc: noticeModel.getReceiptRequirement(param.notice.receiptRequirement).name
        })
      }

      this.setData({
        relations: relations
      })
    }
  },

  /**
   * 签收处理事件函数
   */
  onReceiptMessage:function(event) {
    receiptModel.createReceipt(this, param.notice,event.currentTarget.dataset.relation.student.sid)
  }

})